package com.freak.kotlinfitnessserver.util

import android.os.Build
import androidx.annotation.RequiresApi
import java.text.SimpleDateFormat
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.util.Date

object TimeUtil {
    const val pattern = "yyyy-MM-dd HH:mm:ss"
    fun timeStampToDateWithSecond(timeStamp: Long, pattern: String): String {
        val date = Date(timeStamp * 1000)
        val format = SimpleDateFormat(pattern)
        return format.format(date)
    }
    fun timeStampToDateWithMillisecond(timeStamp: Long, pattern: String): String {
        val date = Date(timeStamp)
        val format = SimpleDateFormat(pattern)
        return format.format(date)
    }

    @RequiresApi(Build.VERSION_CODES.O)
    fun timeStampToDateJava8(timeStamp: Long, pattern: String): String {
        val instant = Instant.ofEpochMilli(timeStamp * 1000)
        val date = LocalDateTime.ofInstant(instant, ZoneId.systemDefault())
        val formatter = DateTimeFormatter.ofPattern(pattern)
        return formatter.format(date)
    }
}